package com.iss.supermarket.EntryUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.entity.Goods;
import com.iss.supermarket.util.repository.GoodsDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class EntryList extends JFrame{
    private static EntryList entryList;
    private int gdNum;
    private JList<Goods> jList;
    private ListModelArray<Goods> listModelArray;
    private JPanel panelPage = new JPanel();
    private JScrollPane jScrollPane =new JScrollPane();

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    private GoodsDao GoodsDao = ctx.getBean("GoodsDao", GoodsDao.class);


    public EntryList(){
        InitUI();
    }

    private void InitUI(){
        Box verticalBox = Box.createVerticalBox();

        jScrollPane.setPreferredSize(new Dimension(400,600));

        JPanel jPanelTableHeader = new JPanel();

        JLabel labelBarCode = new JLabel("条形码");
        labelBarCode.setPreferredSize(new Dimension(100,15));
        jPanelTableHeader.add(labelBarCode);

        JLabel labelBroadName = new JLabel("商品名称");
        labelBroadName.setPreferredSize(new Dimension(100,15));
        jPanelTableHeader.add(labelBroadName);
        jScrollPane.setColumnHeaderView(jPanelTableHeader);

        JLabel labelgdNum = new JLabel("数量");
        labelgdNum.setPreferredSize(new Dimension(100,15));
        jPanelTableHeader.add(labelgdNum);
        jScrollPane.setColumnHeaderView(jPanelTableHeader);

        JLabel labelgdPrice = new JLabel("商品价格");
        labelgdPrice.setPreferredSize(new Dimension(100,15));
        jPanelTableHeader.add(labelgdPrice);
        jScrollPane.setColumnHeaderView(jPanelTableHeader);

        listModelArray = new ListModelArray<Goods>();
        listModelArray.setList(new ArrayList<Goods>());

        jList = new JList<Goods>(listModelArray);
        jScrollPane.setViewportView(jList);
        verticalBox.add(jScrollPane);
        panelPage.add(verticalBox);
    }

    public void flushList(Goods goods){
        int src=0;
        int src1;
        if(listModelArray.getSize() <= 0){
            listModelArray.addList(goods);
        }else if(ifContains(listModelArray,goods)){
            System.out.println(1);
            for(Goods item:listModelArray.getList()){
                if(item.getGdBarCode().equals(goods.getGdBarCode())){
                    src=item.getGdNum();
                    src1=goods.getGdNum();
                    src1+=src;
                    item.setGdNum(src1);
                }
            }
        }else{
            listModelArray.addList(goods);
        }

        jList.setCellRenderer(new ListCellRenderer<Goods> (){
            @Override
            public Component getListCellRendererComponent(JList<? extends Goods> list, Goods value, int index, boolean isSelected, boolean cellHasFocus) {

                JPanel jPanelTableHeader = new JPanel();
                if(cellHasFocus){
                    jPanelTableHeader.setBackground(Color.RED);
                }
                else if(isSelected){
                    jPanelTableHeader.setBackground(Color.BLUE);
                }else{
                    jPanelTableHeader.setBackground(Color.white);
                }

                JLabel labelBarCode = new JLabel(value.getGdBarCode());
                labelBarCode.setPreferredSize(new Dimension(100,20));
                labelBarCode.setFont(new Font("黑体",Font.PLAIN,15));
                jPanelTableHeader.add(labelBarCode);

                JLabel labelBroadName = new JLabel(value.getGdBroadName());
                labelBroadName.setPreferredSize(new Dimension(100,20));
                labelBroadName.setFont(new Font("黑体",Font.PLAIN,15));
                jPanelTableHeader.add(labelBroadName);

                JLabel labelgdNum = new JLabel(String.valueOf(value.getGdNum()));
                labelgdNum.setPreferredSize(new Dimension(100,20));
                labelgdNum.setFont(new Font("黑体",Font.PLAIN,15));
                jPanelTableHeader.add(labelgdNum);

                JLabel labelgdPrice = new JLabel(value.getGdPrice());
                labelgdPrice.setPreferredSize(new Dimension(100,20));
                labelgdPrice.setFont(new Font("黑体",Font.PLAIN,15));
                jPanelTableHeader.add(labelgdPrice);

                return jPanelTableHeader;
            }
        });
    }

    public boolean ifContains(ListModelArray<Goods> list,Goods goods){
        for(int i=0;i<list.getSize();++i){
            if(list.getElementAt(i).getGdBarCode().equals(goods.getGdBarCode())){
                return true;
            }
        }
        return false;
    }

    public static synchronized EntryList getPushStorageList() {
        if(entryList == null){
            entryList = new EntryList();
        }
        return entryList;
    }

    public JPanel getPanelPage() {
        return panelPage;
    }

    public ListModelArray<Goods> getListModelArray(){
        return listModelArray;
    }

    public void Clear(){
        ListModelArray  listModelArray1 = (ListModelArray)jList.getModel();
        listModelArray1.removeAllElements();
        listModelArray1.clearList();
        jList.repaint();
        jList.validate();
    }
}
